#!/usr/bin/perl
$HOME="/home/grm"; require "$HOME/local/scripts/perlstuff-20200606";
use CGI;
$me='http://bbf.sr.unh.edu/~grm/cgi-bin/dashboard.pl';

#
#  ground magnetometer dashboard
#  process files, show inventory, plots
#  JR 2024-02
#

#....... start page
$deb=1;
$q = new CGI;  $da=`date`;
system("/bin/rm 00mess ; touch 00mess");
print $q->header, $q->start_html('CISSMAG Dashboard');
print "<center><h2>UNH CISSMAG Ground Magnetometers Data Dashboard</h2></center>\n";
@mags=suck("maglist.dat");    #..... list of mags and info for them

#.......  all the first call initializations
foreach (qw/update|NO  trange|NONE fgaps|NO ave|NONE withsw|NO trange|last-1d-available datatype|level-2/){
   ($a,$b)=split('\|',$_);  $x=$q->param($a);
    # deb(" ($a,$b)=split('\n',$_);  $x=$q->param($a); ");
    if($x eq ''){ $e="\$$a='$b';";
    deb("|$e|");
    $r=eval($e); } }

#...... get parameters from submit
@pn=$q->param(); foreach $p (@pn){$x=$q->param($p); $e="\$$p='$x';"; $r=eval($e); deb("param $p --> |$x| "); }

  $T1 = $q->hidden(-name => 'T1'); if($T1 < 1111){ $t2=`date "+%Y:%m:%d:%H:%M:%S"`; $T1=epo1t($t2)-24*3600;  }
  $T2 = $q->hidden(-name => 'T2'); if($T2 < 1111){ $t2=`date "+%Y:%m:%d:%H:%M:%S"`; $T2=epo1t($t2);          }
  $tstr1=epo2i($T1);  $tstr2=epo2i($T2);  
  

  print "<br> <pre>
     Usage:
     select time range
     select action (what to do)
            do an update first, it will retrieve the latest data if they are on a different server
            later it should also get DISCOVR data
     select average (in seconds, 0 means no averaging)
     select fillgaps=1 for filling gaps in averaging process
     then click approriate Submit button for the station 

     everything is computed on the fly, so it may take a few seconds.
     JR  2022-09-20  jraeder9 at gmail.com
     T1 T2 $T1 $T2 $tstr1 $tstr2
</pre><br>";

#.......  make table/form
  print "<form method=\"post\" action=\"$me\" enctype=\"multipart/form-data\" name=\"grm-dashboard\">\n";
  print "<input type=\"hidden\" name=\"lasttime\" value=\"$lasttime\" />\n";
    
    
    #..... top table select time range etc
    
    print "<h3>Options:</H3>\n";
    print "<table cellpadding=\"8\">\n";
      TRS();  TD('update',     '#ffcccc'); poptd('update',$update,'NO YES');
              TD('data-type:','#ffcccc');  poptd('datatype',$datatype,'level-2 level-1 level-0');
              TD('Average',   '#ffcccc');  poptd('ave',$ave,'NONE 5 10 20 30 60 120 300 600');
              TD('fillgaps',   '#ffcccc'); poptd('fgaps',$fgaps,'NO YES');  
              TD('with-SW' ,   '#ffcccc'); poptd('withsw', $withsw,'NO YES');  TRE();
              $TRANGE=" last-60s last-10min last-1h last-6h last-1d last-7d last-available-1d ";
              TD('Time-range' ,'#ffcccc'); poptd('trange', $trange,$TRANGE);  TRE();
    print "</table> <br>\n";
    
    #..... table with all stations
    print "<table cellpadding=\"8\">\n";
      TRS();  #..... table header
      TD('Station Name','#ddddff');   #...... static entry
      TD('GEO coordinates','#ffbbff');   #...... static entry
      TD('Location'       ,'#aabbcc');   #...... static entry
      TD('Data range','#ccffff');   #...... static entry
      TD('Submit','#ffaaaa');   #...... static entry
      TRE();
    foreach $mm (@mags){ if($mm =~ /^#/){next;}  @b=split('\|',$mm); $sta=$b[0];
      TRS();
      TD($b[0],'#ddddff');   #...... static entry   name
      TD($b[1],'#ffbbff');   #...... static entry   coordinates
      TD($b[2],'#aabbcc');   #...... static entry   coordinates
      TD('$trange','#ccffff');   #...... static entry  fixme
      SUBMIT1('makeplot','#ffaaaa'); 
      SUBMIT1('Inventory','#ffaaaa'); 
      SUBMIT1('Rawplot','#ffaaaa'); 
      TRE();
    }
  print "</table>\n";
  print "</form>\n";
#....... compute here.  We want to have results/plots after the table

#...... get magnetometer name
@pn=$q->param(); foreach $p (@pn) { $x = $q->param($p); if($p=~/^sub/){ @b=split('\-',$x); $subname=$b[0]; $gm=$b[1]; }}
  $gm=~s/ //g; 
  if($gm eq ''){ print "could not get gm |$sub1|\n";  print $q->end_html,"\n"; exit; }
  foreach $l (@mags){ @b=split('\|',$l); $sta=$b[0]; $sta=~s/ //g; if($sta eq $gm){ $ll=$l; last; }}
#
  deb("PARAMETERS:");
  deb("MAG:  sta|$sta|  gm|$gm|");
  deb("SUBNAME: $subname");
  deb("TRANGE: $trange");  
#
#..... interval

$t2=`date "+%Y:%m:%d:%H:%M:%S"`; $T2=epo1t($t2); $T1=$T2-3600; $t1=epo2i($T1);
   deb("doing |$per| |$sta| |$gm| |$ll| "); deb(" |$T1| |$T2| $t1 $t2\n");

#...... update data
deb("update |$update|");
if($update eq 'YES'){ $ll=~s/ //g; @b=split('\|',$ll); $ll=pop(@b);
   deb("amak.update $gm $ll"); 
   $x=`./amak.update $gm $ll`; 
   deb("amak.update --> $x"); }

if($subInventory =~ /Inventory-(.+)$/){  #...... requested inventory for sta
  $sta=$1;  $x=`./amak.inv $sta`;
  print "<pre>\n$x\n</pre>\n";  endit("finished normal Inventory"); exit; }

if($subRawplot =~ /Rawplot-(.+)$/){  #...... requested plot of L1 data
  $sta=$1;  $x=`./amak.rawplot $sta`; ($pdf,$jpg)=split(',',$x);
      ssy2("/bin/mv $pdf $jpg ..");        #.... apache will not show anything ftom cgi-bin
      $x=`/bin/ls -al ../$pdf ../$jpg`;    print "<pre>\n$x\n</pre>\n";
      print "<br><img src=\"../$jpg\"><br><a href=\"../$pdf\">plot pdf file $pdf</a><br>\n" ;
      endit("finished normal"); }

   endit("finished normal");





   if($action eq 'update'){ ssy2("./amak.update $gm $b[3]");   #.... gets files
      $x=`/bin/ls -1 dir.data/COTS*$gm*/*`;
      foreach $L0 (split('\n',$x)){ mess("update/process: $L0");  if( -e $L0 ){
         $L2=$fin;  $L2=~s/L0/L2/;  $L2=~s/LO/L2/;  mess("L2 file: $L2");
         if( -e $L2 ){ print STDERR "have $L2\n"; next; }
         ssy2("./amak.proc-day $L0");
      } }
         ssy2("./amak.proc-day $L0");  #..... process last one in any case
   }

   if($action eq 'plot_L1'){  $jpg=makeplot(" $gm L1 $Interval plot-L1-$gm "); }
   if($action eq 'plot_L2'){  $jpg=makeplot(" $gm L2 $Interval plot-L2-$gm "); }
   if($action =~ /plot/){ $pdf=$jpg;  $pdf=~s/jpg/pdf/; mess("got image: |$jpg|");
      ssy2("/bin/mv $pdf $jpg .."); #.... apache will not show anything ftom cgi-bin
      $x=`/bin/ls -al ../$pdf ../$jpg`;    print "<pre>\n$x\n</pre>\n";
      print "<br><img src=\"../$jpg\"><br><a href=\"../$pdf\">plot pdf file $pdf</a><br>\n" }

   if($action eq 'list_files'){ $x=`/bin/ls -al dir.data/COTS*$gm*/*`; print "<br><pre>\n";
      foreach(split('\n',$x)){ print "$_\n"; }  print "</pre>\n"; }

   endit("finished normal");

#--------------------------------------
sub makeplot{ local($V)=@_;
#--------------------------------------
  local($gm,$LL,$int,$out)=split(' ',$V);
  mess("makeplot: local($gm,$LL,$int,$out)=split(' ',$V); ");
  local($c);
  $c="font=Courier fsize=6 T1=TT1 T2=TT2 sizex=0.6  sizey=0.4 crop=10 title=$gm-$int-$LL-TT3 verbose=1\n";

  if($LL =~ /L1/){ foreach $w (qw/b1 b2 b3/){ ($t1,$t2,$TT3,$nd)=coldata("$gm L1 $w $int 00tmp.$w");if($nd<11){ endit("found only $nd data points"); }
    $ylabel="$gm.$w";  $c .= " ;  00tmp.$w key=$ylabel ylabel=$ylabel $ex ";  }}

  if($LL =~ /L2/){ foreach $w (qw/bx by bz/){ ($t1,$t2,$TT3,$nd)=coldata("$gm L2 $w $int 00tmp.$w");if($nd<11){ endit("found only $nd data points"); }
    $ylabel="$gm.$w";  $c .= " ;  00tmp.$w key=$ylabel ylabel=$ylabel $ex ";  }}

  $c=~s/TT1/$t1/; $c=~s/TT2/$t2/;  $c=~s/TT3/$TT3/;
  mess("plot: |$c|");
  ssy2("/bin/rm -f tmp.ps tmp.pdf; ./amak.plgnuts-20211030  '$c'");
  $ou="$out-$TT3";
  ssy2("/home/grm/local/scripts/pdfcrop --margins 4 tmp.pdf ; /bin/cp tmp-crop.pdf $ou.pdf");
  ssy2("pdftoppm -singlefile -jpeg -scale-to 1600  $ou.pdf $ou");
  return("$ou.jpg");
}

##......... collect, average, and limit data
#--------------------------------------
sub coldata{ local($V)=@_; 
#--------------------------------------
  local($gm,$LL,$w,$int,$of)=split(' ',$V);
  local($tse,$c,$ca,@LI,$t2,$nb,$d,$r,$k,$pm);
  @LI=glob("dir.data/COTS*${gm}_data/${gm}*$w");
  $c="cat " . join(' ',@LI) . '> tmp.11';  ssy2($c);
  if($int eq 'last1h' ){ $t2=`date "+%Y:%m:%d:%H:%M:%S"`; $T2=epo1t($t2); $T1=$T2-   3600; }
  if($int eq 'last3h' ){ $t2=`date "+%Y:%m:%d:%H:%M:%S"`; $T2=epo1t($t2); $T1=$T2- 3*3600; }
  if($int eq 'last6h' ){ $t2=`date "+%Y:%m:%d:%H:%M:%S"`; $T2=epo1t($t2); $T1=$T2- 6*3600; }
  if($int eq 'last12h'){ $t2=`date "+%Y:%m:%d:%H:%M:%S"`; $T2=epo1t($t2); $T1=$T2-12*3600; }
  if($int eq 'last24h'){ $t2=`date "+%Y:%m:%d:%H:%M:%S"`; $T2=epo1t($t2); $T1=$T2-24*3600; }
  if($int eq 'last1d'){  $t2=`date "+%Y:%m:%d:%H:%M:%S"`; $T2=epo1t($t2); $T1=$T2-24*3600; }
  if($int eq 'last2d'){  $t2=`date "+%Y:%m:%d:%H:%M:%S"`; $T2=epo1t($t2); $T1=$T2-2*24*3600; }
  if($int eq 'last7d'){  $t2=`date "+%Y:%m:%d:%H:%M:%S"`; $T2=epo1t($t2); $T1=$T2-7*24*3600; }
  if($int eq 'last30d'){  $t2=`date "+%Y:%m:%d:%H:%M:%S"`; $T2=epo1t($t2); $T1=$T2-30*24*3600; }
  if($int eq 'last90d'){  $t2=`date "+%Y:%m:%d:%H:%M:%S"`; $T2=epo1t($t2); $T1=$T2-90*24*3600; }
  foreach(split(' ','+1h +6h +24h +2d +7d +30d -1h -6h -24h -7d -30d ')){ if(/^(.)(\d+)(.)/){ $pm=$1; $k=$2; $r=$3;
    if(/$int/){ if($r eq 'h'){$d=$k*3600;} if($r eq 'd'){ $d=$k*3600*24;} if($pm eq '-'){ $d=-$d; }
    mess("coldata shift $_ |$pm| |$k| |$r| $d $T1 $T2"); $T1+=$d; $T2+=$d; last; }}}  #..... shift forward backward
  $tstr1=epo2i($T1);  $tstr2=epo2i($T2);  
  print  "coldata: $t2 $T1 $T2  $tstr1  $tstr2 |$d|\n";
  print  "<br><br>coldate: $t2 $T1 $T2  $tstr1  $tstr2 |$d|<br>\n";
  @LI=(); @LI=suck("tmp.11"); $nb=0;
  $z=''; $nb=0;  foreach(@LI){ @b=split(); $TT=epo1(@b); if(($TT>$T1)&&($TT<$T2)){ $nb++; $z.="$_\n"; }} dumpf($z,$of);
  $tsa="/home/grm/local/bin/tser_ave -t1 $tstr1 -t2 $tstr2 -tav $ave ";
  if(($ave>0)&&($fgaps>0)){ ssy2("cat $of | $tsa  -fillgaps > tmp.9; /bin/mv tmp.9 $of"); }
  if(($ave>0)&&($fgaps<1)){ ssy2("cat $of | $tsa  +fillgaps > tmp.9; /bin/mv tmp.9 $of"); }
  $TT3=$tstr1; $TT3=~s/://g; $TT3=substr($TT3,0,14); return(($tstr1,$tstr2,$TT3,$nb));
}

sub endit{  local($c)=@_; #.i...... end page
  print "<br>The End: $c <br> $da<br>\n";
  $x=`cat 00mess`; print "<pre>Messages:\n$x\n</pre>\n";
  print $q->end_html,"\n";
}

sub deb{ local($x)=@_; if($deb<1){ return;} print "<pre>$x</pre>\n"; }
sub TD{ local($w,$c)=@_; print "<td bgcolor=\"$c\">$w</td>\n"; }
sub TRS{ print "<tr>\n"; }  
sub TRE{ print "</tr>\n"; }  
sub SUBMIT1{ local($s,$c)=@_;
   print "<td bgcolor=\"$c\">\n<input type=\"submit\" name=\"sub$s\" value=\"$s-$sta\" />\n </td>\n";  }
sub mess{ local($x)=@_; open(MESS,">>/home/grm/public_html/cgi-bin/00mess"); print MESS "$x\n"; close(MESS); }
#------------------------------------------
sub poptd{ 
#------------------------------------------
#.... makes a table entry with select menu
  local($name,$sel,$v,@v);
  $name=shift(@_); $sel=shift(@_); $v=shift(@_);  @v=split(' ',$v);
  print "<td>\n"; print "<select name=\"$name\" >\n";
  foreach(@v){
    if($_ eq $sel){ print "<option selected=\"selected\" value=\"$sel\">$_</option>\n"; }
    if($_ ne $sel){ print "<option value=\"$_\">$_</option>\n"; }
  }
  print "</select>\n</td>\n";  }

sub ssy2{ local($c)=@_;  mess("ssy2: |$c|\n"); system($c);  return; }
sub ssy{ local($c)=@_; print STDERR "ssy:|$c|\n"; system($c); return; }
sub min{ local($a,$b)=@_;local($c); $c=$a; if($b<$a){$c=$b;} return($c); }
sub max{ local($a,$b)=@_;local($c); $c=$a; if($b>$a){$c=$b;} return($c); }
sub abs{ local($a)=@_;local($c); $c=$a; if($a<0){$c=-$a;} return($c); }
sub log10{local($a)=@_;$y10=1.0/(log(10.0));$a=$y10*log(max($a,1.0e-15)); return($a); }
sub log2{local($a)=@_;$y10=1.0/(log(2.0));$a=$y10*log(max($a,1.0e-15)); return($a); }
sub basename{ local($k)=@_; local(@a)=split('\/',$k); return($a[$#a]); }
sub suck{local($f)=@_; local($t,@t);$t=`cat $f`;@t=split('\n',$t); print STDERR "suck: |$f| |$#t|\n"; return(@t); }
sub dumpf{ local($c,$f)=@_; open(DF,">$f"); print DF $c; close(DF); }
sub dumpa{ local($f)=shift(@_); open(FF,">$f");print FF join("\n",@_); close(FF); }
sub mes{ local($x)=@_; print STDERR ">$x<\n"; }
sub err{ local($x)=@_; print STDERR "error $0 message: |$x|\n"; exit; }
__END__
